package com.tmc.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class BaseInfoTest {
	
	public static String outputPath = "C:\\Users\\legion\\Desktop\\今日基础数据.xlsx";

	public static void main(String[] args) throws Exception {
		File file = new File("C:\\Users\\legion\\Desktop\\今日基础数据.txt");
		String result = "";
		InputStream in = null;
		BufferedReader bufferedReader = null;
		try {
			in = new FileInputStream(file);
			InputStreamReader reader = new InputStreamReader(in, "GBK");
			bufferedReader = new BufferedReader(reader);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				result += lineTxt;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				bufferedReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		JSONObject obj = JSON.parseObject(result);
		JSONArray array = obj.getJSONArray("AirLine");
		write(array);
	}
	
	public static void write(JSONArray array) throws Exception {
		File file = new File(outputPath);
		// 声明一个工作薄
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 境内
		processJingnei(workbook, array);
		// 境外
		processJingwai(workbook, array);
		workbook.write(new FileOutputStream(file));
	}
	
	public static void processJingwai (XSSFWorkbook workbook, JSONArray array){
		JSONObject jingnei = array.getJSONObject(1);
		JSONArray zhous = jingnei.getJSONArray("Data");
		for (int i =0;i<zhous.size();i++){
			JSONObject zhou = zhous.getJSONObject(i);
			JSONArray quyus = zhou.getJSONArray("Data");
			for (int m =0;m<quyus.size();m++) {
				JSONObject quyu = quyus.getJSONObject(m);
				String label = quyu.getString("Label");
				Sheet sheet = workbook.createSheet(label);
				JSONArray countrys = quyu.getJSONArray("Data");
				for (int q =0;q<countrys.size();q++){
					Row row = sheet.createRow(q);
					JSONObject country = countrys.getJSONObject(q);
					String countryname = country.getString("Label");
					String countryCode = country.getString("Key");
					Cell cell0 = row.createCell(0);
					System.out.println(countryname);
					cell0.setCellValue(countryCode + "-" + countryname);
					JSONArray airports = country.getJSONArray("Data");
					StringBuffer sb = new StringBuffer();
					for (int k =0;k<airports.size();k++) {
						JSONObject airport = airports.getJSONObject(k);
						String airportCode = airport.getString("Key");
						String airportName = airport.getString("Label");
						sb.append(airportCode + "-" + airportName).append("/");
					}
					Cell cell1 = row.createCell(1);
					cell1.setCellValue(sb.toString());
				}
			}
		}
	}
	
	public static void processJingnei (XSSFWorkbook workbook, JSONArray array){
		JSONObject jingnei = array.getJSONObject(0);
		String label = jingnei.getString("Label");
		JSONArray dataArray = jingnei.getJSONArray("Data");
		JSONArray shengs = dataArray.getJSONObject(0).getJSONArray("Data");
		Sheet sheet = workbook.createSheet(label);
		for (int i =0;i<shengs.size();i++) {
			Row row = sheet.createRow(i);
			JSONObject sheng = shengs.getJSONObject(i);
			String name = sheng.getString("Label");
			Cell cell = row.createCell(0);
			cell.setCellValue(name);
			JSONArray airports = sheng.getJSONArray("Data");
			StringBuffer sb = new StringBuffer();
			for (int k =0;k<airports.size();k++) {
				JSONObject airport = airports.getJSONObject(k);
				String airportCode = airport.getString("Key");
				String airportName = airport.getString("Label");
				sb.append(airportCode + "-" + airportName).append("/");
			}
			Cell cell1 = row.createCell(1);
			cell1.setCellValue(sb.toString());
		}
	}
}
